/**
 * @author David Milne
 */

function DocumentsTray() {
	logDebug("DocumentsTray created");
	
	this.loading ;
	this.contentArea ;
	this.xmlHttp ;
	
	this.documents = new Array() ;
	this.currDocHash = null ;
}

DocumentsTray.prototype.init = documentsTray_init ;
DocumentsTray.prototype.resize = documentsTray_resize ;
DocumentsTray.prototype.setLoading = documentsTray_setLoading ;

DocumentsTray.prototype.openDocument = documentsTray_openDocument ;
DocumentsTray.prototype.tabSelectionChanged = documentsTray_tabSelectionChanged ;
DocumentsTray.prototype.tabRemoved = documentsTray_tabRemoved ;

DocumentsTray.prototype.requestDocumentContent = documentsTray_requestDocumentContent ;
DocumentsTray.prototype.processDocumentContentResponse = documentsTray_processDocumentContentResponse ;
DocumentsTray.prototype.getIndexOfDocument = documentsTray_getIndexOfDocument ;
DocumentsTray.prototype.getDocHashFromIndex = documentsTray_getDocHashFromIndex ;



function documentsTray_init() {
	try {
		this.id = "documentsTray" ;
		this.className = "tray" ;
		
		var title = document.createElement("img") ;
		title.className = "title" ;
		title.src = "images/documentsTray.gif" ;
		this.appendChild(title) ;
		
		var tl = document.createElement("div") ;
		tl.className = "border_tl" ;
		this.appendChild(tl) ;
		
		var tr = document.createElement("div") ;
		tr.className = "border_tr" ;
		this.appendChild(tr) ;
		
		var bl = document.createElement("div") ;
		bl.className = "border_bl" ;
		this.appendChild(bl) ;
		
		var br = document.createElement("div") ;
		br.className = "border_br" ;
		this.appendChild(br) ;
		
		this.loading = document.createElement("div") ;
		this.loading.className = "loading" ;
		this.appendChild(this.loading) ;
		this.setLoading(false) ;
		
		this.tabPane = document.applyInherit(document.createElement('div'), new TabPane()) ;
		this.tabPane.init(new Function("koru.documentsTray.tabSelectionChanged()"), new Function("koru.documentsTray.tabRemoved()"), LEFT, 150) ;
		this.appendChild(this.tabPane) ;
		
		this.contentArea = document.createElement("div") ;
		this.contentArea.className = "contentArea" ;
		this.appendChild(this.contentArea) ;
		
	} catch ( e ) { 
		logError("documentsTray.init.error: " + e ); 
		throw (e) ;
	}
} 
  
function documentsTray_resize(width, height) {
	try {
		this.style.width = width - 4 + "px" ;
		this.style.height = height - 4 + "px" ;
		
		this.contentArea.style.width = width-24 + "px" ;
		this.contentArea.style.height = height-74 + "px" ;
		
		this.tabPane.resize(width-4) ;
	} catch (e) {
		logError("documentsTray.resize.error: " + e) ;
		throw (e) ;
	}
}

function documentsTray_setLoading(loading) {
	if (loading==true) 
		this.loading.style.display = "" ;
	else
		this.loading.style.display = "none" ;
}


function documentsTray_openDocument(docTitle, docHash) {
	try {
		
		var index = this.getIndexOfDocument(docHash) ;
		
		if (index >= 0) {
			this.tabPane.selectTab(docHash) ;
		} else {
			
			this.requestDocumentContent(docHash) ;
			
			var doc = document.applyInherit(document.createElement('div'), new DocumentPage()) ;
			doc.init(docTitle, docHash) ;
			this.documents.push(doc) ;
			this.contentArea.appendChild(doc) ;
			
			this.tabPane.addTab(docHash, docTitle) ;
			this.tabPane.selectTab(docHash) ;
		}
				
	} catch (e) {
		logError("documentsTray.openDocument.error: " + e) ;
		throw (e) ;
	}
}

function documentsTray_tabSelectionChanged() {
	try {
		logDebug("Tab selection changed to:" + this.tabPane.currTabId) ;
		
		for(var i=0 ; i<this.documents.length ; i++) {
			var doc = this.documents[i] ;
			if (doc.docHash == this.tabPane.currTabId)
				doc.style.display = "" ;
			else
				doc.style.display = "none" ;
		}
		
		//logDebug("docNum:" + docNum + " docs:" + this.documents.length + " currDoc:" + this.currDocHash) ; 			
			
	} catch (e) {
		logError("documentsTray.tabSelectionChanged.error: " + e) ;
		throw (e) ;
	}
}

function documentsTray_tabRemoved() {
	try {
		var docHash = this.tabPane.removedTabId ;
		//this.tabPane.removedTabId = null ;
		logDebug("Removing document " + docHash) ;
		
		var docIndex = this.getIndexOfDocument(docHash) ;

		this.contentArea.removeChild(this.documents[docIndex]) ;
		this.documents.splice(docIndex, 1) ;
	} catch (e) {
		logError("documentsTray.tabRemoved.error: " + e) ;
		throw (e) ;
	}
}

function documentsTray_requestDocumentContent(hash) {
	try {
		this.setLoading(true) ;
		
  		var externalId = newElementAtt('param', '', new Array('name', 'value'), new Array('ext', '0') ) ; 
  		var dHash = newElementAtt('param', '', new Array('name', 'value'), new Array('docHash', hash) ) ; 
 		var taskId = newElementAtt('param', '', new Array('name', 'value'), new Array('taskId', koruGame.task_id) ) ;
		var query = "<query>" + koru.searchBox.getCurrentQuery() + "</query>" ;
		
		
  		var paramList = newElement('paramList', externalId + dHash + taskId) ;
  		var request = newElementAtt('request', paramList + query, new Array('lang', 'to', 'type', 'uid'), new Array('en', collection + '/GetDocument', 'process', '1'  ) );
    
  		var message = newElement('message', request );
  
  		var soapMessage = messageToSOAP(message);
  
  		this.xmlHttp = XmlHttp.create();
  		this.xmlHttp.open("POST", "http://" + serverPath + "/services/localsite", true);
  		this.xmlHttp.onreadystatechange = new Function("koru.documentsTray.processDocumentContentResponse(\"" + hash + "\") ; ") ;
   
  		this.xmlHttp.setRequestHeader("SOAPAction", "\"\"");
  		this.xmlHttp.setRequestHeader("Content-Type", "Content-Type: text/xml; charset=utf-8");
 
  		this.xmlHttp.send(soapMessage);
	}catch (e) {
  		logError("documentsTray.requestDocumentContent.error: " + e ); 
  		throw (e) ; 
 	}
}

function documentsTray_processDocumentContentResponse(hash) {
	try {
  		if (this.xmlHttp.readyState == 4) {
			logDebug("response for doc: " + hash) ;
			
			var documentNode = this.xmlHttp.responseXML.getElementsByTagName('Document')[0] ;
			var relevance = documentNode.getAttribute("relevance") ;
			var id = documentNode.getAttribute("id") ;
			
			koruGame.documentOpened(id, relevance) ;
			
   			var documentText = documentNode.getAttribute("text") ;
			
			var numTerms = documentNode.getAttribute("numTerms") ;
			
			
			for (var i=0; i < numTerms ; i++) {
				var color = koru.getHighlightColor(i) ;
			
				documentText = replace(documentText, "color" + i, color) ;
			}
			
			var docIndex = this.getIndexOfDocument(hash) ;
			
			if (docIndex >=0)
				this.documents[docIndex].setContent(documentText) ;
		
			this.setLoading(false) ;
		}
	}catch (e) {
  		logError("documentsTray.processDocumentContentResponse.error: " + e ); 
  		throw (e) ; 
 	}
}

function documentsTray_getIndexOfDocument(docHash) {
	try {
  		for(var i=0 ; i<this.documents.length;i++) {
			
   			if (this.documents[i].docHash == docHash) {
				logDebug("index for doc:" + docHash + " = " + i) ;
				return i ;
			}
    			
  		}
		logDebug("index for doc:" + docHash + " = -1") ;
		return -1 ;
	} catch (e) {
		logError( "documentsTray.getIndexOfDocument.error: " + e ); 
		throw (e) ; 
	}
}

function documentsTray_getDocHashFromIndex(docIndex) {
	try {
		logDebug("Finding doc hash for index" + docIndex) ;
  		var doc = this.documents[docIndex] ;
		return doc.docHash ;
	} catch (e) {
		logError( "documentsTray.getDocHashFromIndex.error: " + e ); 
		throw (e) ; 
	}
}
  